﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace YTSK.Model
{
    public class ResultJson
    {
        private int _result;
        private string _resultdetail;
        private Dictionary<string, object> _retvalue;

        /// <summary>
        /// 是否成功 1：成功  -1 ：失败
        /// </summary>
        public int result
        {
            get { return _result; }
            set { _result = value; }
        }

        /// <summary>
        /// 描述信息
        /// </summary>
        public string resultdetail
        {
            get { return _resultdetail; }
            set { _resultdetail = value; }
        }

        public Dictionary<string, object> value
        {
            get { return _retvalue; }
            set { _retvalue = value; }
        }

        public object GetVal(string name)
        {
            if (_retvalue.Keys.Contains(name))
            { return _retvalue[name]; }
            else { return null; }
        }

        public T Get<T>(string name)
        {
            if (_retvalue == null) { return default(T); }
            if (!value.ContainsKey(name)) { return default(T); }
            var obj = (Dictionary<string, object>)value[name];
            return obj.ToObj<T>();
        }

        /// <summary>
        /// 覃豪--20170212--魏小伟--20170214
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<T> GetList<T>(string name)
        {
            if (_retvalue == null) { return default(List<T>); }
            if (!value.ContainsKey(name)) { return default(List<T>); }
            var objori=value[name];
            ArrayList obj;
            if (objori is ArrayList) 
            {
                obj = (ArrayList)objori;
            }
            else
            {
                obj = new ArrayList() { objori };
            }
            List<T> list = new List<T>();
            foreach (var item in obj)
            {
                var dic = (Dictionary<string, object>)item;
                list.Add(dic.ToObj<T>());
            }
            return list;
            //var obj = (List<Dictionary<string, object>>)value[name];
            //return obj.ToList<T>();
        }
        public List<Dictionary<string, object>> GetListDic(string name)
        {
            var list = new List<Dictionary<string, object>>();
            if (_retvalue == null) { return list; }
            if (!value.ContainsKey(name)) { return list; }
            var obj = (ArrayList)value[name];
            list = ArrayToDic(obj);
            return list;
            //var obj = (List<Dictionary<string, object>>)value[name];
            //return obj.ToList<T>();
        }

        public List<Dictionary<string, object>> ArrayToDic(ArrayList obj)
        {
            var list = new List<Dictionary<string, object>>();
            var controlvalue = new List<Dictionary<string, object>>();
            foreach (var item in obj)
            {
                var dic = (Dictionary<string, object>)item;
                list.Add(dic);
            }
            return list;
        }

    }
}
